package com.bw.paper.mvp.view.Fragment.people;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.paper.App;
import com.bw.paper.mvp.view.Activity.PeopleValuesActivity;
import com.bw.paper.mvp.view.Fragment.people.adapter.FollowNewsAdapter;
import com.bw.paper.mvp.view.Fragment.people.adapter.UserFollowAdapter;
import com.bw.paper.R;
import com.bw.paper.entity.FollowEntity;
import com.bw.paper.entity.UserEntity;
import com.bw.paper.entity.UserFollowEntity;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseFragment;
import com.bw.paper.mvp.contact.LREContract;
import com.bw.paper.mvp.di.DaggerLREComponent;
import com.bw.paper.mvp.di.LREModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.presenter.LREPresenter;
import com.bw.paper.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

public class FollowFragment extends BaseFragment<LREPresenter> implements
        LREContract.ILREView, OnRefreshLoadMoreListener, OnItemClickListener {

    private SmartRefreshLayout follow_smart;
    private RecyclerView follow_news_rv;
    private View headView;
    private RecyclerView follow_author_rv;
    private LinearLayoutManager author,news;
    private FollowNewsAdapter followNewsAdapter;
    private UserFollowAdapter userFollowAdapter;



    @Override
    public int bindlayout() {
        return R.layout.fragment_follow;

    }

    @Override
    public void initView() {
        follow_smart = f(R.id.follow_smart);
        follow_news_rv = f(R.id.follow_news_rv);
        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_follow_head,null);
        follow_author_rv = headView.findViewById(R.id.follow_author_rv);
        follow_smart.setEnableLoadMore(true);
        follow_smart.setEnableRefresh(true);
        follow_smart.setRefreshHeader(new ClassicsHeader(getContext()));
        follow_smart.setRefreshFooter(new ClassicsFooter(getContext()));
        follow_smart.setOnRefreshLoadMoreListener(this);
        news = new LinearLayoutManager(getContext());
        news.setOrientation(LinearLayoutManager.VERTICAL);
        follow_news_rv.setLayoutManager(news);
        author = new LinearLayoutManager(getContext());
        author.setOrientation(LinearLayoutManager.HORIZONTAL);
        follow_author_rv.setLayoutManager(author);
        userFollowAdapter = new UserFollowAdapter();
        followNewsAdapter = new FollowNewsAdapter();
        followNewsAdapter.addHeaderView(headView);
        follow_news_rv.setAdapter(followNewsAdapter);
        follow_author_rv.setAdapter(userFollowAdapter);
        userFollowAdapter.setOnItemClickListener(this::onItemClick);
    }

    @Override
    public void initData() {
        UserEntity entity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
        Map<String,Object>newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.FOLLOWNEWSCODE);
        newsMap.put("user_id",1);
        Map<String,Object>followMap = new HashMap<>();
        followMap.put("code",HttpCode.USERFOLLOWCODE);
        followMap.put("user_id",1);
        p.requestAll(followMap,newsMap);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this))
                .build().injectFollowFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof FollowEntity){
            FollowEntity followEntity = (FollowEntity) entity;
            followNewsAdapter.setNewInstance(followEntity.getValues());
        }else {
            UserFollowEntity userFollowEntity = (UserFollowEntity) entity;
            userFollowAdapter.setNewInstance(userFollowEntity.getValues());
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        FollowEntity followEntity = (FollowEntity) entity;
        if (followEntity.getValues() == null){
            ToastUtils.showMsg("刷新失败");
            return;
        }
        followNewsAdapter.setNewInstance(followEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        FollowEntity followEntity = (FollowEntity) entity;
        if (followEntity.getValues() == null){
            ToastUtils.showMsg("最后一页");
            return;
        }
        followNewsAdapter.addData(followEntity.getValues());
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        UserEntity entity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
        Map<String,Object>newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.FOLLOWNEWSCODE);
        newsMap.put("user_id",1);
        p.requestLoad(newsMap);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        UserEntity entity = App.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
        Map<String,Object>newsMap = new HashMap<>();
        newsMap.put("code", HttpCode.FOLLOWNEWSCODE);
        newsMap.put("user_id",1);
        p.requestRefresh(newsMap);
    }



    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        startPage(PeopleValuesActivity.class);
    }
}